<template>
    <Form :label-width="80" >
        <div style="min-width:1000px;max-width:1100px;">
        <div class="inline-blocks align-middle">
            <FormItem label="用户名：" prop="name">
                <!--<div style="display:inline-block;width:240px;">
                    <Input placeholder="请输入用户名" clearable v-model="logArgs.search.userName"/>
                </div>-->
                <Select v-model="logArgs.search.userName" clearable filterable placeholder="请选择用户名" style="width:240px;">
                    <Option v-for="item in searchTb" :value="item.key" :key="item.key">{{ item.label }}</Option>
                </Select>
            </FormItem>
        </div>
        <div class="inline-blocks align-middle">
            <FormItem label="操作时间：">
                <DatePicker type="daterange" placement="bottom-end" placeholder="请选择操作时间" style="width: 240px" @on-change="selectTime($event)"></DatePicker>
            </FormItem>
        </div>
        <div class="inline-blocks align-middle">
            <FormItem label="操作结果：">
                <div style="display:inline-block;width:240px;">
                    <Select v-model="logArgs.search.operationResult" clearable>
                        <Option v-for="item in moduleResult" :value="item.value" :key="item.value">{{ item.label }}</Option>
                    </Select>
                </div>
            </FormItem>
        </div>
        <!--<div class="inline-blocks">
            <FormItem label="姓名：" prop="name">
                <div style="display:inline-block;width:240px;">
                    <Input placeholder="请输入姓名" clearable v-model="logArgs.search.account"/>
                </div>
            </FormItem>
        </div>-->
        <div class="inline-blocks align-middle">
            <FormItem label="事件：" prop="name">
                <div style="display:inline-block;width:240px;">
                    <Select v-model="logArgs.search.event" clearable >
                        <Option v-for="item in eventList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                    </Select>
                </div>
            </FormItem>
        </div>
        <div class="inline-blocks align-middle">
            <FormItem label="日志类型：">
                <div style="display:inline-block;width:240px;">
                    <Select v-model="logArgs.search.logType" clearable >
                        <Option v-for="item in moduleStatus" :value="item.value" :key="item.value">{{ item.label }}</Option>
                    </Select>
                </div>
            </FormItem>
        </div>
        <div class="inline-blocks align-middle" style="margin-bottom: 32px;margin-left: 17px;">
            <!--<FormItem label="日志内容：">
                <div style="display:inline-block;width:240px;">
                    <Select v-model="logArgs.search.id" clearable filterable placeholder="请选择日志内容">
                        <Option v-for="item in this.searchTbs" :value="item.key" :key="item.key">{{item.values}}-{{item.label}}</Option>
                    </Select>
                </div>
            </FormItem>-->
            <span class="middle-32 label">日志内容：</span>
            <Input style="width:240px" placeholder="请输入日志内容" clearable v-model="logArgs.search.logContent"/>
        </div>
        <div class="inline-blocks align-middle">
            <FormItem label="模块：">
                <div style="display:inline-block;width:240px;">
                    <Select v-model="logArgs.search.module" clearable>
                        <Option v-for="item in modules" :value="item.value" :key="item.value">{{item.label}}</Option>
                    </Select>
                </div>
            </FormItem>
        </div>
        <Button  type="primary"  v-show="hasButtonPermission('101010501')"  @click="getLogTabl" style="display:inline-block;margin-top:-25px;margin-left:20px;">查询</Button>
        </div>
        <div class="margin-bottom-20">
            <Table border :columns="logColumns" :data="logData"></Table>
        </div>
        <Page :total="total" :current="currentPage" size="small" show-elevator show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
        <Spin size="large" fix v-if="spinShow">加载中...</Spin>
    </Form>
</template>

<script>
    import {getLogTableApi,postEmployeeNameApi} from '../../http/log.api'
    import * as employeeOpr from '../../http/employee.api'
    export default {
        name: 'Log',
        data () {
            return {
                total: 0,
                spinShow: false,
                resultArry: [],
                searchTb: [],
                searchTbs: [],
                nameArry:[],
                nameList: {
                    userNames: 'system' // 用户名集合
                },
                // 查询条件
                logArgs: {
                    search: {
                        operationTimeStart: '',//操作开始时间
                        operationTimeEnd: '',//操作结束时间
                        userName: '',//操作人用户名
                        account:'',// 操作人帐户
                        event: '',//操作事件
                        /*eventCode: '',//操作事件code*/
                        logType: '',//日志类型
                        logContent: '',//日志内容
                        operationResult:'',//操作结果
                        module:''//模块
                    },
                    pageReqired: true,
                    limit: 10,
                    offset: 10,            // 偏移量
                    /*sort: 'operationTime',*/   // 排序的字段
                    /*order: 'desc',  */      // asc 升序 desc 降序
                },
                nameArgs: [],
                moduleStatus: [
                    {
                        value: 'user',
                        label: '用户'
                    },
                    {
                        value: 'system',
                        label: '系统'
                    }
                ],
                modules:[
                    {
                        value: 'usermgt',
                        label: '用户管理模块'
                    },
                    {
                        value: 'logistics_engine',
                        label: '物流引擎'
                    }
                ],
                moduleResult: [
                    {
                        value: 'success',
                        label: '成功'
                    },
                    {
                        value: 'failed',
                        label: '失败'
                    },
                    {
                        value: 'error',
                        label: '错误'
                    }
                ],
                eventList:[
                    {
                        value: 'login',
                        label: '登录'
                    },
                    {
                        value: 'logout',
                        label: '退出'
                    },
                    {
                        value: 'role_delete',
                        label: '角色删除'
                    },
                    {
                        value: 'role_update',
                        label: '角色修改'
                    },
                    {
                        value: 'role_create',
                        label: '角色创建'
                    },
                    {
                        value: 'orgfunc_upd',
                        label: '职能更新'
                    },
                    {
                        value: 'orgfunc_add',
                        label: '职能新增'
                    },

                    {
                        value: 'orgfunc_del',
                        label: '职能删除'
                    },
                    {
                        value: 'orgfunc_copy',
                        label: '职能复制'
                    },
                    {
                        value: 'orgfunc_data',
                        label: '职能关联数据权限'
                    },
                    {
                        value: 'orgfunc_emp',
                        label: '职能关联用户'
                    },
                    {
                        value: 'employee_create',
                        label: '添加员工'
                    },
                    {
                        value: 'employee_eidtperson',
                        label: '编辑个人资料'
                    },
                    {
                        value: 'employee_modipwd',
                        label: '修改个人密码'
                    },
                    {
                        value: 'employee_update',
                        label: '编辑员工'
                    },
                    {
                        value: 'employee_assoroles',
                        label: '员工设置角色'
                    },

                    {
                        value: 'orgstruct_create',
                        label: '添加组织架构'
                    },
                    {
                        value: 'orgstruct_delete',
                        label: '删除组织架构'
                    },
                    {
                        value: 'orgstruct_setems',
                        label: '设置组织架构对应人员'
                    },
                    {
                        value: 'orgstruct_update',
                        label: '编辑组织架构'
                    },
                    {
                        value: 'shippingMethod_insert',
                        label: '物流方式新增'
                    },
                    {
                        value: 'shippingMethod_update',
                        label: '物流方式修改'
                    },
                    {
                        value: 'shippingMethod_delete',
                        label: '物流方式删除'
                    },
                    {
                        value: 'shippingMethod_batch_delete',
                        label: '物流方式批量删除'
                    },
                    {
                        value: 'shippingMethod_update_priority',
                        label: '修改优先级'
                    },
                    {
                        value: 'shippingMethod_batch_enable',
                        label: '批量设置启用/禁用'
                    },
                    {
                        value: 'shippingMethodLevel_insert',
                        label: '物流级别新增'
                    },
                    {
                        value: 'shippingMethodLevel_update',
                        label: '物流级别修改'
                    },
                    {
                        value: 'shippingMethodLevel_delete',
                        label: '物流级别删除'
                    },
                    {
                        value: 'shippingMethodLevel_batch_delete',
                        label: '物流级别批量删除'
                    },
                    {
                        value: 'shippingMethodCalculation_insert',
                        label: '算价信息新增'
                    },
                    {
                        value: 'shippingMethodCalculation_update',
                        label: '算价信息修改'
                    },
                    {
                        value: 'shippingMethodCalculation_delete',
                        label: '算价信息删除'
                    },
                    {
                        value: 'shippingMethodCalculation_batch_delete',
                        label: '算价信息批量删除'
                    },
                    {
                        value: 'shippingMethodCalculation_batch_import',
                        label: '算价信息批量导入'
                    }
                ],
                logColumns: [
                    {
                        title: '用户名',
                        key: 'userName'
                    },
                    {
                        title: '姓名',
                        key: 'account'
                    },
                    {
                        title: '事件code',
                        key: 'eventCode'
                    },
                    {
                        title: '事件',
                        key: 'event'
                    },
                    {
                        title: '用户ip ',
                        key: 'ip'
                    },
                    {
                        title: '日志类型',
                        key: 'logType',
                        width:150
                    },
                    {
                        title: '日志内容',
                        key: 'logContent',
                        width:350
                    },
                    {
                        title: '模块',
                        key: 'module'
                    },
                    {
                        title: '操作结果',
                        key: 'operationResult',
                        width:150
                    },
                    {
                        title: '操作时间',
                        key: 'operationTime',
                        render: (h, params) => {
                            if(!(params.row.operationTime)){
                                return h('div', '')
                            }else {
                                var text = this.$moment(params.row.operationTime).format('YYYY-MM-DD HH:mm:ss')
                                return h('div', text)
                            }
                        }
                    }
                ],
                //查询列表
                logData: [],
                currentPage: 1, // 当前页
                isItems: false
            }
        },
        mounted() {
            this.getEmployeeNames();
        },
        created () {
            this.getLogTable()
        },
        /*filters: {
            capitalize: function (value) {
                if (!value) return ''
                value = value.toString()
                return value.charAt(0).toUpperCase() + value.slice(1)
            }
        },*/
        methods:{
            hasButtonPermission(permission) {
                return this.$store.getters['login/hasButtonPermission'](permission);
            },
            getLogTabl(){
                this.pageChanges(1)
                this.getLogTable ()
            },
            // 查询日志列表
            getLogTable () {
                this.nameArgs = []
                this.spinShow = true
                this.logArgs.offset = (this.currentPage - 1) * this.logArgs.limit
                const search = this.logArgs.search
                search.operationTimeStart = (search.operationTimeStart ? this.$moment(search.operationTimeStart).format('YYYY-MM-DDTHH:mm:ss') :  undefined)
                search.userName = search.userName ? search.userName :  undefined
                search.account = search.account ? search.account :  undefined
                search.event = search.event ? search.event :  undefined
                /*search.eventCode ? null :  delete search.eventCode*/
                search.logType = search.logType ? search.logType :  undefined
                search.operationTimeEnd = (search.operationTimeEnd ? this.$moment(search.operationTimeEnd).format('YYYY-MM-DDTHH:mm:ss') :  undefined)
                search.operationResult = search.operationResult ? search.operationResult :  undefined
                search.module = search.module ? search.module :  undefined
                console.log("search.operationTimeStart===="+this.$moment(search.operationTimeStart).format('YYYY-MM-DDTHH:mm:ss'));
                this.logArgs.search = search
                getLogTableApi(this.logArgs).then(resp => {
                    console.log("resp=="+resp)
                    resp.data.rows.map(list => {
                        if(list.logType=="system"){
                            list.logType = '系统'
                        }else if(list.logType=="user"){
                            list.logType = '用户'
                        }

                        //开始时间
                        const TimeStart= this.$moment(this.logArgs.search.operationTimeStart).format('YYYY-MM-DD')
                        //结束时间
                        const TimeEnd= this.$moment(this.logArgs.search.operationTimeEnd).format('YYYY-MM-DD')
                        //返回的时间段
                        const tionTime= this.$moment(list.operationTime).format('YYYY-MM-DD')

                        if(list.operationResult=="success"){
                            list.operationResult = '成功'
                        }else if(list.operationResult=="failed"){
                            list.operationResult = '失败'
                        }else if(list.operationResult=="error"){
                            list.operationResult = '错误'
                        }
                        if(list.module=="101"){
                            list.module = '用户管理模块'
                        }
                    })

                    this.total = resp.data.total
                    const _this = this
                    postEmployeeNameApi(resp.data.rows.map(item => (item.userName)?(item.userName):'')).then(suc => {
                        _this.resultArry = suc.data.result
                        _this.nameArry = Object.keys(_this.resultArry).map(key => ({key, label: `${_this.resultArry[key]||''}`}));

                        resp.data.rows.map(list => {
                            if(list.module=='usermgt'){
                                list.module = '用户管理模块'
                            }else if(list.module=='logistics_engine'){
                                list.module = '物流引擎'
                            }

                            if(list.event=='login'){
                                list.event = '登录'
                            }else if(list.event=='logout'){
                                list.event = '退出'
                            }else if(list.event=='test'){
                                list.event = '失败'
                            }else if(list.event=='role_delete'){
                                list.event = '角色删除'
                            }else if(list.event=='role_update'){
                                list.event = '角色修改'
                            }else if(list.event=='role_create'){
                                list.event = '角色创建'
                            }else if(list.event=='orgfunc_upd'){
                                list.event = '职能更新'
                            }else if(list.event=='orgfunc_add'){
                                list.event = '职能新增'
                            }else if(list.event=='orgfunc_del'){
                                list.event = '职能删除'
                            }else if(list.event=='orgfunc_copy'){
                                list.event = '职能复制'
                            }else if(list.event=='orgfunc_data'){
                                list.event = '职能关联数据权限'
                            }else if(list.event=='orgfunc_emp'){
                                list.event = '职能关联用户'
                            }else if(list.event=='employee_create'){
                                list.event = '添加员工'
                            }else if(list.event=='employee_eidtperson'){
                                list.event = '编辑个人资料'
                            }else if(list.event=='employee_modipwd'){
                                list.event = '修改个人密码'
                            }else if(list.event=='employee_update'){
                                list.event = '编辑员工'
                            }else if(list.event=='employee_assoroles'){
                                list.event = '员工设置角色'
                            }else if(list.event=='orgstruct_create'){
                                list.event = '添加组织架构'
                            }else if(list.event=='orgstruct_delete'){
                                list.event = '删除组织架构'
                            }else if(list.event=='orgstruct_setems'){
                                list.event = '设置组织架构对应人员'
                            }else if(list.event=='orgstruct_update'){
                                list.event = '编辑组织架构'
                            }else if(list.event=='shippingMethod_insert'){
                                list.event = '物流方式新增'
                            }else if(list.event=='shippingMethod_update'){
                                list.event = '物流方式修改'
                            }else if(list.event=='shippingMethod_delete'){
                                list.event = '物流方式删除'
                            }else if(list.event=='shippingMethod_batch_delete'){
                                list.event = '物流方式批量删除'
                            }else if(list.event=='shippingMethod_update_priority'){
                                list.event = '修改优先级'
                            }else if(list.event=='shippingMethod_batch_enable'){
                                list.event = '批量设置启用/禁用'
                            }else if(list.event=='shippingMethodLevel_insert'){
                                list.event = '物流级别新增'
                            }else if(list.event=='shippingMethodLevel_update'){
                                list.event = '物流级别修改'
                            }else if(list.event=='shippingMethodLevel_delete'){
                                list.event = '物流级别删除'
                            }else if(list.event=='shippingMethodLevel_batch_delete'){
                                list.event = '物流级别批量删除'
                            }else if(list.event=='shippingMethodCalculation_insert'){
                                list.event = '算价信息新增'
                            }else if(list.event=='shippingMethodCalculation_update'){
                                list.event = '算价信息修改'
                            }else if(list.event=='shippingMethodCalculation_delete'){
                                list.event = '算价信息删除'
                            }else if(list.event=='shippingMethodCalculation_batch_delete'){
                                list.event = '算价信息批量删除'
                            }else if(list.event=='shippingMethodCalculation_batch_import'){
                                list.event = '算价信息批量导入'
                            }
                        })
                        this.logData = resp.data.rows
                        this.logData.map(listName => {
                            _this.nameArry.map(names => {
                            if(listName.userName==names.key) {
                                listName.account = names.label
                            }
                            })
                        })

                    }).catch(err => {
                        console.log("err= "+err)
                        /*this.$Message.error("错误代码【"+err.data.code+"】，"+err.data.message)*/
                        this.spinShow = false
                    })
                    this.spinShow = false
                }).catch(err => {
                    console.log("err= "+err)
                    this.$Message.error("错误代码【"+err.data.code+"】，"+err.data.message)
                    this.spinShow = false
                })
            },
            // 切换页码
            pageChange (n) {
                this.currentPage = n
                this.getLogTable()
            },
            // 切换页码1
            pageChanges (n) {
                this.currentPage = n
            },
            // 切换每页显示数量
            pageSizeChange (n) {
                this.logArgs.limit = n
                this.getLogTable()
            },
            getEmployeeNames(args) {
                const _this = this;
                employeeOpr.getEmployeeNames(args).then(function (res) {
                    const data = res.data.result;
                    _this.searchTb = Object.keys(data).map(key => ({key, label: `${key}-${data[key]||''}`}));
                }).catch(function (err) {
                    console.log(err)
                })
            },
            // 选择时间
            selectTime(time){
                const _this = this;
                var date = new Date(time[1])
                if(time[0] === time[1]){
                    var time1 = date.getTime()+86400000;
                    var time11= this.$moment(time1).format('YYYY-MM-DD')
                }
                _this.logArgs.search.operationTimeStart = time[0]
                _this.logArgs.search.operationTimeEnd = time11
                console.log("开始时间", _this.logArgs.search.operationTimeStart)
                console.log("结束时间", _this.logArgs.search.operationTimeEnd)
            }
        }
    }
</script>
